package com.sniff.atp.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RandomUtil {


    //指定长度的中文字符
    public static String chineseString(int length) {
        StringBuilder strTemp = new StringBuilder();
        for (int i = 0; i < length; i++) {
            String str = null;
            int highPos, lowPos; // 定义高低位
            Random random = new Random();
            highPos = (176 + Math.abs(random.nextInt(39)));
            lowPos = (161 + Math.abs(random.nextInt(93)));
            byte[] b = new byte[2];
            b[0] = (new Integer(highPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                str = new String(b, "GBk");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            strTemp.append(str);
        }
        return strTemp.toString();
    }

    //指定长度的大写英文字符
    public static String upperCase(int length) {
        StringBuilder strTemp = new StringBuilder();
        for (int i = 0; i < length; i++) {
            String str = String.valueOf((char) (Math.round(Math.random() * 25) + 65));
            strTemp.append(str);
        }
        return strTemp.toString();
    }

    //指定长度的小英文字符
    public static String lowCase(int length) {
        StringBuilder strTemp = new StringBuilder();
        for (int i = 0; i < length; i++) {
            String str = String.valueOf((char) (Math.round(Math.random() * 25) + 97));
            strTemp.append(str);
        }
        return strTemp.toString();
    }

    //指定长度的数字
    public static String numberString(int length) {

//        StringBuilder strTemp = new StringBuilder();
//        for (int i = 0; i < length; i++) {
//            strTemp.append(Math.round(Math.random() * 9));
//        }
//        return strTemp.toString();

        return String.valueOf(new Date().getTime()).substring(0, length);
    }

    //指定长度的数字
    public static String numberStringNoZero(int length) {
        StringBuilder strTemp = new StringBuilder();
        for (int i = 0; i < length; i++) {
            strTemp.append(Math.round(Math.random() * 8) + 1);
        }
        return strTemp.toString();
    }


    //指定长度的混合字符，英文，数字，符号
    public static String mixedString(int length) {
        StringBuilder strTemp = new StringBuilder();
        for (int i = 0; i < length; i++) {
            strTemp.append((char) (Math.round(Math.random() * 92) + 33));
        }
        return strTemp.toString();
    }

    //阿拉伯数字转中文数字
    public static String arabianToChinese(String sArabian) {
        StringBuilder strTemp = new StringBuilder();
        String[] strChinese = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        for (int i = 0; i < sArabian.length(); i++) {
            String str = strChinese[Integer.parseInt(String.valueOf(sArabian.charAt(i)))];
            strTemp.append(str);
        }
        return strTemp.toString();
    }

    //判断是否是正整数
    public static boolean isNaturalNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    //判断是否是整数
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^-?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    //判断是否是数字
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    public static String randomString(String str, int ilength) {
        if (StringUtils.isNotBlank(str)) {
            Random random = new Random();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ilength; i++) {
                int iCharLocaltion = random.nextInt(ilength);
                sb.append(str.charAt(iCharLocaltion));
            }
            return sb.toString();
        }
        return str;
    }

    public static String randomMailTail() {
        String[] commonEmailSuffixs = {"gmail.com", "yahoo.com", "msn.com",
                "hotmail.com", "aol.com", "ask.com", "live.com", "qq.com",
                "0355.net", "163.com", "163.net", "263.net", "3721.net",
                "yeah.net", "googlemail.com", "mail.com", "aim.com",
                "walla.com", "inbox.com", "126.com", "sina.com", "21cn.com",
                "sohu.com", "yahoo.com.cn", "tom.com", "etang.com", "eyou.com",
                "56.com", "x.cn", "chinaren.com", "sogou.com", "citiz.com",
                "hongkong.com", "ctimail.com", "hknet.com", "netvigator.com",
                "mail.hk.com", "swe.com.hk", "itccolp.com.hk",
                "biznetvigator.com", "seed.net.tw", "topmarkeplg.com.tw",
                "pchome.com.tw"};

        Random ran = new Random();
        int iEmailIndex = ran.nextInt(commonEmailSuffixs.length - 1);
        return commonEmailSuffixs[iEmailIndex];
    }
}
